<?php
namespace app\index\controller;

class Index
{




    //获取微信token
    public function get_token() :string{
        $appid="  ";//微信api_id
        $appsecret="  ";//微信秘钥

       $result= $this->geturl("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret");
       $token=$result['access_token'];
       return $token;
    }

    //发送
    public function send() :void{
        $token=$this->get_token();
        $api_array=$this->set_api();

        $data=[
            "touser"=>"",//要发送的用户
            "template_id"=>"",//模板id
            "topcolor"=>"#FF0000",
            "data"=>[
                "date"=>[
                    "value"=>$api_array['date'],
                    "color"=>"#f97306"
                ],
                "lunar"=>[
                    "value"=>$api_array['lunar'],
                    "color"=>"#873f05"
                ],
                "festival"=>[
                    "value"=>$api_array['festival'],
                    "color"=>"#f80707"
                ],
                "city"=>[
                    "value"=>$api_array['city'],
                    "color"=>"#071ef8"
                ],
                "weather"=>[
                    "value"=>$api_array['weather'],
                    "color"=>"#07f89a"
                ],
                    "temp"=>[
                    "value"=>$api_array['temp'],
                    "color"=>"#07f8ef"
                ],
                "day"=>[
                    "value"=>$api_array['send_money_day'],
                    "color"=>"#f90606"
                ]
            ]


        ];

        $result=$this->posturl("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$token",$data);
        print_r($result);
    }

    //组装api
    public function set_api() :array{

        $weather_api_key="  ";//天气api的秘钥
        $city="  ";//需要获取天气的城市
        $weather_res=$this->geturl("https://api.muxiaoguo.cn/api/tianqi?api_key=$weather_api_key&city=$city&type=1");
        $temp=$weather_res['data']['temp'];//温度
        $weather=$weather_res['data']['weather'];//天气状况

        $lunar_api_key="  ";//农历api秘钥
        $lunar_res=$this->geturl("https://api.muxiaoguo.cn/api/yinlongli?api_key=$lunar_api_key");
        $lunar=$lunar_res['data']['lunar'];//农历日期
        $festival_res=$lunar_res['data']['festival'];//节日数组，可能会有很多节日

        $festival="";//这个不需要你填写
        for ($i=0;$i<count($festival_res);$i++){
            $festival .=$festival_res[$i].",";
        }

        date_default_timezone_set('PRC');
        $date= date("Y-m-d");//公历日期
        $weekArray=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
        $weekNumber=date("w");
        $week=$weekArray[$weekNumber];
        $dates=$date.$week;//当前年月日加星期

        $nowday=date("j");//当前天数，无前导
        $nowMonth=date("n");//当前月，无前导
        $nowDate=strtotime($date);//当前时间戳
        $send_money=8;//每月发工资的日期
        if ($nowday>=$send_money){
            //那么说明当月工资以发过，
            $nextMonth=$nowMonth+1;//下个月
            $send_money_time=strtotime(date("Y-$nextMonth-$send_money"));//发工资的时间戳
            $day= $day=($send_money_time-$nowDate)/86400;//剩余发工资的天数


        }else{
            //工资还没发
            $send_money_time=strtotime(date("Y-m-$send_money"));//发工资的时间戳
            $day=($send_money_time-$nowDate)/86400;//剩余发工资的天数
        }





        return [
            "temp"=>$temp."°C",
            "date"=>$dates,
            "weather"=>$weather,
            "lunar"=>$lunar,
            "send_money_day"=>$day,
            "festival"=>$festival,
            "city"=>$city
        ];
    }


  public  function geturl($url){
        $headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);
        return $output;
    }

   public function posturl($url,$data){
        $data  = json_encode($data);
        $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output,true);
    }
}
